package com.putao.album.guide;

import android.os.Handler;
import android.view.View;
import android.widget.ImageView;

import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;
import com.putao.album.R;
import com.putao.album.application.GlobalApplication;
import com.putao.album.base.BaseFragment;
import com.putao.album.main.AlbumHomeActivity;
import com.putao.album.user.ActivityLoginRegister;
import com.putao.album.util.ActivityHelper;
import com.putao.album.util.PuTaoConstants;
import com.putao.album.util.SharedPreferencesHelper;
import com.putao.album.util.StringHelper;
import com.putao.album.util.ViewHelper;

/**
 * Created by jidongdong on 15/4/23.
 */
public class SplashFragment extends BaseFragment implements IGuideAnimation {
    private ImageView splash_img_cube, splash_img_icon, splash_img_word_1, splash_img_word_2;

    @Override
    public int doGetContentViewId() {
        return R.layout.fragment_splash_layout;
    }

    @Override
    public void doInitSubViews(View view) {
        super.doInitSubViews(view);
        splash_img_cube = ViewHelper.queryViewById(view, R.id.splash_img_cube);
        splash_img_icon = ViewHelper.queryViewById(view, R.id.splash_img_icon);
        splash_img_word_1 = ViewHelper.queryViewById(view, R.id.splash_img_word_1);
        splash_img_word_2 = ViewHelper.queryViewById(view, R.id.splash_img_word_2);
        Play();
    }

    @Override
    public void doInitDataes() {
    }

    void intoApp() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final String current_user = SharedPreferencesHelper.readStringValue(GlobalApplication.getInstance(), PuTaoConstants.PREFERENCE_KEY_MOBILE, null);
                ActivityHelper.startActivity(mActivity, StringHelper.isEmpty(current_user) ? ActivityLoginRegister.class : AlbumHomeActivity.class);
                getActivity().finish();
            }
        }, 500);
    }


    @Override
    public void Play() {
        //icon动画
        ObjectAnimator icon_alpha = getEeletmetsAnimator(splash_img_icon, 1400, 0);
        //背景动画
        ObjectAnimator cube_alpha = getEeletmetsAnimator(splash_img_cube, 1600, 400);
        cube_alpha.addListener(listener);
        //文字1动画
        ObjectAnimator word_1_alpha = getEeletmetsAnimator(splash_img_word_1, 1400, 200);
        //文字2动画
        ObjectAnimator word_2_alpha = getEeletmetsAnimator(splash_img_word_2, 1400, 400);
        //播放动画
        AnimatorSet set = new AnimatorSet();
        set.setStartDelay(200);
        set.playTogether(icon_alpha, cube_alpha, word_1_alpha, word_2_alpha);
        set.start();
    }

    @Override
    public void Move(int distance) {

    }

    ObjectAnimator getEeletmetsAnimator(Object obj, int duration, int delay) {
        ObjectAnimator ani = ObjectAnimator.ofFloat(obj, "alpha", 0f, 1f).setDuration(duration);
        ani.setStartDelay(delay);
        return ani;
    }

    Animator.AnimatorListener listener = new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            intoApp();
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    };

}
